package com.vast.ui.lcdpanel.views;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.vast.ui.lcdpanel.models.SwtImageLayer;

public class ViewPart extends org.eclipse.ui.part.ViewPart {

	public ViewPart() {
		// TODO Auto-generated constructor stub
	}
	
	public  InputStream getInputStream(String resource) {  
        
        InputStream stream = this.getClass().getResourceAsStream( resource );  
        if ( stream == null ) stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( resource );  
  
        if ( stream == null ) {  
            File file = new File(resource);  
            try {  
                return new FileInputStream(file);  
            } catch (Exception ex) {  
                //  
            }  
              
            try {  
                URL sourceUrl = new URL(resource);  
                if (sourceUrl != null) {  
                    stream = sourceUrl.openStream();  
                }  
            } catch (Exception e) {  
                return null;  
            }  
        }  
                  
        return stream;  
    }
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		//final Canvas canvas = new Canvas(parent, SWT.NONE);
		final Display display = parent.getDisplay();
		
		SwtImageRenderer imageRenderer = new SwtImageRenderer(parent);
		
		
		byte[] rawData = new byte[320 * 240 * 3];
		for( int i = 0; i < 320 *240 *3; i += 3) {
			rawData[i] = (byte)0xFF;
			rawData[i + 1] = (byte)0x0;
			rawData[i + 2] = (byte)0xF;
		}
		SwtImageLayer imageLayer = new SwtImageLayer(parent);
		imageLayer.createDirectColorImageData(320, 240, 24, -1, rawData);
		imageRenderer.add(imageLayer);
		
		InputStream inputStream = getInputStream("picture/overview_64bit20090824.png");
	    Image image = new Image(display, inputStream);
	    ImageData imageData = image.getImageData();
	    imageLayer = new SwtImageLayer(parent);
	    imageLayer.setImageData(imageData);
	    imageRenderer.add(imageLayer);
	    
	    imageRenderer.setVisible(0, true);
	    imageRenderer.setVisible(1, true);
		
		
		
		//canvas.addPaintListener(new PaintListener() {
		//      public void paintControl(PaintEvent e) {
		//    	  
		//    	 InputStream inputStream = getInputStream("picture/overview_accelerated20090824.png");
		//        Image image = new Image(display, inputStream);
		//        ImageData data = image.getImageData();
		//        Rectangle bound = canvas.getClientArea();

		//        int y;
		//        int offset = 20;
		//        e.gc.drawImage(image, 0, 0);
		//        e.gc.drawText("scanlinePad :" + Integer.toString(data.scanlinePad), 0, 0);
		//        e.gc.drawText("bytesPerLine :" + Integer.toString(data.bytesPerLine), 0, 20);
		//        e.gc.drawText("width :" + Integer.toString(data.width), 0, 40);
		//        e.gc.drawText("height :" + Integer.toString(data.height), 0, 60);
		//        e.gc.drawText("depth :0x" + Integer.toHexString(data.depth), 0, 80);
		//        e.gc.drawText("alpha :0x" + Integer.toHexString(data.alpha), 0, 100);

		//        image.dispose();
		//        
		//      }
		//   });
		    
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
